package com.multi.thread.chapter04.demo11;

import java.util.concurrent.locks.ReentrantLock;

/**
 *	tryLock():仅在调用时锁定未被另一个线程保持的情况下，才获取该锁定
 */
public class TestTryLock {
	
	private ReentrantLock lock = new ReentrantLock();
	
	public void waitMethod() {
		if (lock.tryLock()) {
			System.out.println(Thread.currentThread().getName() + "获得锁!");
		} else {
			System.out.println(Thread.currentThread().getName() + "没有获得所!");
		}
	}
	
	public static void main(String[] args) {
		final TestTryLock service = new TestTryLock();
		Runnable run = new Runnable() {
			@Override
			public void run() {
				service.waitMethod();
			}
		};
		Thread a = new Thread(run);
		a.setName("A");
		a.start();
		Thread b = new Thread(run);
		b.setName("B");
		b.start();
	}

}
